package com.jason.ladder.webapp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.jason.ladder.Constants;
import com.jason.ladder.model.Game;
import com.jason.ladder.model.User;
import com.jason.ladder.service.GameManager;
import com.jason.ladder.service.UserExistsException;
import com.jason.ladder.service.UserManager;

@Controller
public class ContestantController {
	
	@Autowired
	UserManager userManager;
	
	@Autowired
	GameManager gameManager;


	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView doGet2(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("play");
		mav.addObject("list", userManager.getUsersByRank());
		//List<User> tempUsers = userManager.getUsersByRank();
		//tempUsers.remove((User)request.getSession().getAttribute(Constants.SESSION_USER));
		User curUser = (User)request.getSession().getAttribute(Constants.SESSION_USER);
		mav.addObject("yourId", curUser.getId());
		//mav.addObject("request", request);
		return mav;
		
	}
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView doJson(HttpServletRequest request, HttpServletResponse response){
		User you1 = (User) request.getSession().getAttribute(Constants.SESSION_USER);
		
		String youId = you1.getId().toString();
		String contestantId = request.getParameter("contestant");
		Long yourScore = Long.parseLong(request.getParameter("yourScore"));
		Long contestantScore = Long.parseLong(request.getParameter("contestantScore"));
		Boolean affectLadder = false;
		if(request.getParameter("affect").equalsIgnoreCase("on")){
			affectLadder = true;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-d");
		
		Date playDate = new Date();
		
		try {
			playDate = df.parse(request.getParameter("dateOfPlay"));
			
		} catch (Exception e) {
			
			//e.printStackTrace();
		}
		//Save game for you
		Game yourGame = new Game(playDate, Long.parseLong(youId), Long.parseLong(contestantId),yourScore,contestantScore);
		gameManager.saveGame(yourGame);
		//Save game for opponent
		Game opponentGame = new Game(playDate, Long.parseLong(youId), Long.parseLong(contestantId),yourScore,contestantScore);
		gameManager.saveGame(opponentGame);
		if(affectLadder){
			User you = userManager.getUser(youId);
			User contestant = userManager.getUser(contestantId);
			Integer youCurrentPlace = you.getCurrentPlace();
			Integer contestantCurrentPlace = contestant.getCurrentPlace();
			if(you.getCurrentPlace() < contestant.getCurrentPlace()){
				if(contestantScore > yourScore){
					you.setCurrentPlace(contestantCurrentPlace);
					contestant.setCurrentPlace(youCurrentPlace);
				}
			}else if(contestant.getCurrentPlace() < you.getCurrentPlace()){
				if(yourScore > contestantScore){
					you.setCurrentPlace(contestantCurrentPlace);
					contestant.setCurrentPlace(youCurrentPlace);
				}
			}
			try {
				userManager.save(you);
				userManager.save(contestant);
			} catch (UserExistsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	
		
		ModelAndView mav = new ModelAndView("outputView");
		
		return mav;
		
	}
	
}
